<template>
  <section>
    <!--工具条-->
    <!-- <div>{{winWidth}}</div>
    <div>{{winHeight}}</div> -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <!-- <el-row :gutter="40"> -->
        <!-- <el-col :xs="24" :sm="12" :md="6" :lg="6" style="margin-bottom:5px;"> -->
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.shareId" placeholder="分享标识"></el-input>
        </el-form-item>
        <!-- </el-col> -->
        <!-- <el-col :xs="24" :sm="12" :md="6" :lg="6" style="margin-bottom:5px;"> -->
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.userCode" placeholder="分享人"></el-input>
        </el-form-item>
        <!-- </el-col> -->
        <!-- <el-col :xs="24" :sm="12" :md="6" :lg="6" style="margin-bottom:5px;"> -->
        <el-form-item>
          <el-input size="small" style="width:200px;" v-model.trim="filters.agrementNo" placeholder="业务关系代码"></el-input>
        </el-form-item>
        <!-- </el-col> -->
        <!-- <el-col :xs="24" :sm="12" :md="6" :lg="6" style="margin-bottom:5px;"> -->
        <el-form-item>
          <el-date-picker size="small" style="width:200px;" v-model="filters.Dates" type="daterange" align="left" placeholder="选择日期范围">
          </el-date-picker>
        </el-form-item>
        <!-- </el-col> -->

        <!-- </el-row> -->
        <el-form-item style="float:right;">
          <el-button size="small" v-show="exportBtnVisible" type="primary" icon="upload2" @click="exportExcels">导出</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" v-show="addBtnVisible" type="primary" icon="plus" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" v-show="queryBtnVisible" type="primary" icon="search" v-on:click="getLogs(true)">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="Logs" stripe border :height="winHeight-188" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;" :cell-style="{height:'10px',color:'#ccc'}">
      <!-- <el-table-column type="selection" width="55">
      </el-table-column> -->
      <!-- <el-table-column prop="serialNo" label="序号" width="120" sortable>
      </el-table-column> -->
      <el-table-column prop="shareId" align="center" label="分享标识" width="120" show-overflow-tooltip>
        <!-- <el-tooltip class="item" effect="dark" :content="scope.row.shareId" placement="top"> -->
        <!-- <template scope="scope">
            <span style="height:20px;">{{scope.row.shareId}}</span>
          </template> -->
        <!-- </el-tooltip> -->
      </el-table-column>
      <el-table-column prop="shareType" align="center" label="分享类型" width="100">
      </el-table-column>
      <el-table-column prop="productId" align="center" label="产品ID" width="100" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="agrementNo" align="center" label="业务关系代码" min-width="150">
      </el-table-column>
      <el-table-column prop="saleName" align="center" label="销售人员" width="100" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="saleCode" align="center" label="销售人员编码" min-width="130" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="sharinChain" align="center" label="点击次数" width="100">
      </el-table-column>
      <el-table-column prop="userCode" align="center" label="分享人" width="120" show-overflow-tooltip>
      </el-table-column>

      <el-table-column prop="createTime" align="center" label="分享时间" min-width="160" sortable show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="lastShareType" align="center" label="最后一次分享类型" width="150">
      </el-table-column>
      <el-table-column prop="updateTime" align="center" label="更新时间" min-width="160" sortable show-overflow-tooltip>
      </el-table-column>

      <el-table-column prop="shareNum" align="center" label="分享次数" width="150">
      </el-table-column>
      <el-table-column prop="refShareId" align="center" label="分享来源" width="150">
      </el-table-column>

      <el-table-column label="操作" fixed='right' width="100">
        <template scope="scope">
          <!-- <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button> -->
          <el-button type="danger" v-show="deleteBtnVisible" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->

      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="120px" :rules="addFormRules" ref="addForm">
        <el-row>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="分享标识:" prop="name">
              <el-input v-model="addForm.shareId" style="width:100%;" auto-complete="off" placeholder="请输入分享标识">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="分享类型:" prop="name">
              <el-input v-model="addForm.shareType" style="width:100%;" auto-complete="off" placeholder="请输入分享标识">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="产品ID:" prop="name">
              <el-input v-model="addForm.productId" style="width:100%;" auto-complete="off" placeholder="请输入产品ID">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="业务关系代码:" prop="name">
              <el-input v-model="addForm.agrementNo" style="width:100%;" auto-complete="off" placeholder="请输入业务关系代码">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="销售人员:" prop="name">
              <el-input v-model="addForm.saleName" style="width:100%;" auto-complete="off" placeholder="请输入销售人员">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="销售人员编码:" prop="name">
              <el-input v-model="addForm.saleCode" style="width:100%;" auto-complete="off" placeholder="请输入销售人员编码">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="点击次数:" prop="name">
              <el-input v-model="addForm.sharinChain" style="width:100%;" auto-complete="off" placeholder="请输入点击次数">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="分享人编码:" prop="name">
              <el-input v-model="addForm.userCode" style="width:100%;" auto-complete="off" placeholder="请输入分享人编码">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="销售渠道名称:" prop="name">
              <el-input v-model="addForm.channelName" style="width:100%;" auto-complete="off" placeholder="请输入销售渠道名称">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="分享时间:" prop="name">
              <el-date-picker v-model="addForm.createTime" type="datetime" style="width:100%;" placeholder="请选择分享时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="状态:" prop="name">
              <el-input v-model="addForm.state" style="width:100%;" auto-complete="off" placeholder="请输入状态">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="18" :md="12" :lg="12">
            <el-form-item label="userCodeChain:" prop="name">
              <el-input v-model="addForm.userCodeChain" style="width:100%;" auto-complete="off" placeholder="请输入userCodeChain">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'

import commons from '../../common/js/common'
//import NProgress from 'nprogress'
import { mapState, mapActions } from 'vuex'
import { shareList, addShare, removeShare } from '../../api/api';

export default {
  data() {
    return {
      filters: {
        shareId: '',
        userCode: '',
        agrementNo: '',
        Dates: ''
      },
      addForm: {
        shareId: '',
        shareType: '',
        productId: '',
        agrementNo: '',
        channelName: '',
        createTime: '',
        saleCode: '',
        saleName: '',
        sharinChain: '',
        state: '',
        userCode: '',
        userCodeChain: ''
      },
      Logs: [],
      lists: [],
      exportTotal: 0,
      total: 0,
      page: 1,
      pageSize: 10,
      loading: '',
      // fullscreenLoading:false,
      listLoading: false,
      addLoading: false,
      addFormVisible: false,
      sels: [],//列表选中列
      userid: '',
      addFormRules: {

      },
      IsShow: false,

      queryBtnVisible: false,
      addBtnVisible: false,
      exportBtnVisible: false,
      deleteBtnVisible: false
    }
  },
  computed: mapState([
    'winWidth',
    'winHeight'
  ]),
  methods: {
    cellStyle(row, rowIndex, columnIndex) {
    },
    formatStatus: function (row, column) {
      return row.level == '1' ? '一级' : (row.level == '2' ? '二级' : '三级');
    },
    formatTime: function (row, column) {
      return commons.getDaytime(row.createTime, 1, false);
    },
    formatUpdateTime: function (row, column) {
      return row.updateTime ? commons.getDaytime(row.updateTime, 1, false) : "";
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getLogs(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getLogs(true);
    },
    //获取用户列表
    getLogs(bool) {
      let para = new FormData();
      para.append("pageSize", bool ? this.pageSize : this.exportTotal);
      para.append("pageNumber", bool ? this.page : 1);
      para.append("shareId", this.filters.shareId);
      para.append("userCode", this.filters.userCode);
      para.append("agrementNo", this.filters.agrementNo);
      para.append("startTime", commons.changeTimes(this.filters.Dates, 'start'));
      para.append("endTime", commons.changeTimes(this.filters.Dates, 'end'));
      this.listLoading = bool;
      // this.fullscreenLoading = bool?false:true;
      if (!bool) {
        this.loading = this.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }
      shareList(para).then((res) => {
        this.lists = res.data.result.content;
        this.total = res.data.result.totalElements;
        this.exportTotal = res.data.result.totalElements;
        this.listLoading = false;
        if (!bool) {
          this.loading.close();
          this.export2Excel();
        }
        else {
          this.Logs = res.data.result.content;
        }
      });
    },
    //table导出excel
    exportExcels: function () {
      this.getLogs(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = ['分享标识', '分享类型', '产品ID', '业务关系代码', '销售人员', '销售人员编码', '创建用户', '点击次数', '创建时间'];
        const filterVal = ['shareId', 'shareType', 'productId', 'agrementNo', 'saleName', 'saleCode', 'userCode', 'sharinChain', 'createTime'];
        const list = this.lists;
        // for (var i in list) {
        //   list[i].createTime = commons.getDaytime(list[i].createTime, 1, false);
        // }
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '分享列表excel');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    //显示新增界面
    handleAdd: function () {
      this.addFormVisible = true;
      this.addForm = {
        shareId: '',
        shareType: '',
        productId: '',
        agrementNo: '',
        channelName: '',
        createTime: '',
        saleCode: '',
        saleName: '',
        sharinChain: '',
        state: '',
        userCode: '',
        userCodeChain: ''
      };
    },
    //新增提交
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            let para = this.addForm;
            para.createTime = Date.parse(para.createTime);
            addShare(para).then((res) => {
              if (res.data.success) {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
                this.$refs['addForm'].resetFields();
                this.addFormVisible = false;
                this.getLogs(true);
              }
              else {
                this.$message({
                  message: res.data.result,
                  type: 'error'
                });
              }
              this.addLoading = false;
            }).catch(() => {
              this.addLoading = false;
            });
          });
        }
      });
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = new FormData();
        para.append("id", row.id);
        removeShare(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getLogs(true);
        });
      }).catch(() => {

      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },

    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('146'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "shareQuery") {
          _this.queryBtnVisible = true;
        }
        if (thisData[i] == "shareAdd") {
          _this.addBtnVisible = true;
        }
        if (thisData[i] == "shareExport") {
          _this.exportBtnVisible = true;
        }
        if (thisData[i] == "shareRemove") {
          _this.deleteBtnVisible = true;
        }
      });
    }
  },
  mounted() {
    this.getLogs(true);
    this.btnShow();
  }
}

</script>

<style scoped>

</style>